module.exports = ({ app }) => {
  
  return async function (ctx, next) {
    try {
      await next()
    } catch (error) {
      // log
      // app.emit('error', error, ctx)
      const status = error.status || 500
      const errMsg = app.env === 'production'
        ? 'Internal Server Error'
        : error.message

      ctx.body = {
        code: status,
        error: errMsg
      }
    }
  }
}